package com.shire.shire_interface.controller.stat;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.stat.CsGroupStatExample;
import com.shire.shire_interface.service.stat.CsGroupStatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//@Api(tags = "统计数据")
@RestController
@RequestMapping("/stat")
public class CsGroupStatController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(CsGroupStatController.class);

    @Resource
    private CsGroupStatService groupStatService;

//    @ApiOperation(value = "获取群数据统计，参数以json body post. csId, accounts:帐号列表，groupName(模糊匹配), startDate, endDate")
    @ResponseBody
    @PostMapping("/getGroupStat")
    public Response getGroupStat(@RequestBody JSONObject bodyJson, HttpSession session){
        Response response = new Response();

        try{

            CsGroupStatExample example = new CsGroupStatExample();
            CsGroupStatExample.Criteria criteria = example.createCriteria();

            Integer csId = bodyJson.getInteger("csId");
            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            criteria.andCsidEqualTo(csId);

            if (bodyJson.containsKey("accounts")) {
                List<String> accounts = new ArrayList<>();
                for (Object o : bodyJson.getJSONArray("accounts")) {
                    accounts.add((String) o);
                }
                criteria.andWxaccountIn(accounts);
            }

            if (bodyJson.containsKey("groupName")) {
                String groupName = bodyJson.getString("groupName");
                criteria.andGroupnameLike("%"+groupName+"%");
            }

            if (bodyJson.containsKey("startDate")) {
                String startDate = bodyJson.getString("startDate");
                criteria.andDtGreaterThanOrEqualTo(startDate);
            }
            if (bodyJson.containsKey("endDate")) {
                String endDate = bodyJson.getString("endDate");
                criteria.andDtLessThanOrEqualTo(endDate);
            }

            example.setOrderByClause("dt desc");

            response.setData(groupStatService.selectByExample(example));

            response.success("操作成功");

        }catch (Exception e){
            e.printStackTrace();
            response.error("操作失败");
        }

        return response;
    }
}
